﻿using System;
using System.Linq.Expressions;
using System.Windows.Controls;
using System.Windows;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using SL8.SL.Helpers;

namespace SL8.SL.Controls
{
    [TemplatePart(Name = EditTextSearchItem.PART_SearchTextBox, Type = typeof(TextBox))]
    [TemplatePart(Name = EditTextSearchItem.PART_CategoryListBox, Type = typeof(ListBox))]
    public class EditTextSearchItem : SearchItemBase
    {
        private const string PART_CategoryListBox = "CategoryListBox";
        private const string PART_SearchTextBox = "SearchTextBox";

        private TextBox SearchTextBox
        {
            get;
            set;
        }

        private ListBox CategoryListBox
        {
            get;
            set;
        }

        public EditTextSearchItem()
        {
            this.DefaultStyleKey = typeof(EditTextSearchItem);
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            this.SearchTextBox = this.GetTemplateChild(EditTextSearchItem.PART_SearchTextBox) as TextBox;
            this.CategoryListBox = this.GetTemplateChild(EditTextSearchItem.PART_CategoryListBox) as ListBox;
        }

        public override System.Linq.Expressions.LambdaExpression GetSearchExpression()
        {
            System.Linq.Expressions.LambdaExpression expression = null;

            if (!string.IsNullOrWhiteSpace(this.SearchTextBox.Text))
            {
                if (this.CategoryListBox != null && this.CategoryListBox.ItemsSource != null)
                {
                    foreach (var item in this.CategoryListBox.ItemsSource)
                    {
                        if (item is SearchColumnInfo)
                        {
                            SearchColumnInfo searchColumnInfo = (SearchColumnInfo)item;

                            if (searchColumnInfo.IsChecked)
                            {
                                System.Linq.Expressions.LambdaExpression expressionTemp = null;

                                if (this.CompareType == CompareType.Equal)
                                {
                                    expressionTemp = LinqExpressionHelper.GetEqualExpressionByEntityType(this.ParameterExpression,
                                          searchColumnInfo.ColumnName,
                                          this.SearchTextBox.Text,
                                          this.EntityType);
                                }
                                else if (this.CompareType == CompareType.Contains)
                                {
                                    expressionTemp = LinqExpressionHelper.GetContainsExpressionByEntityType(this.ParameterExpression,
                                          searchColumnInfo.ColumnName,
                                          this.SearchTextBox.Text,
                                          this.EntityType);
                                }

                                if (expressionTemp != null)
                                {
                                    if (expression == null)
                                    {
                                        expression = expressionTemp;
                                    }
                                    else
                                    {
                                        expression = System.Linq.Expressions.Expression.Lambda(System.Linq.Expressions.Expression.OrElse(expression.Body, expressionTemp.Body), expression.Parameters);
                                    }
                                }
                            }
                        }
                    }
                }
            }

            return expression;
        }

        public override void SubscribeToTemplatePartEvents()
        {
            if (this.SearchTextBox != null)
            {

            }
        }
    }
}
